package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;

/**
 * Test case for the DueDates class. Tests for successful login, unsuccessful login, etc. Does not
 * test main method.
 * 
 * @author Scheller Sanchez and Tyler Wolff
 */

public class TestDueDatesGold {

  /**
   * Test null parameters in getUhInfo.
   * 
   * @exception Exception for null parameters.
   */
  @Test(expected = Exception.class)
  public void testGetUhInfoException() throws Exception {
    DueDatesGold dueDate = new DueDatesGold();
    dueDate.getUhInfo(null, null, null);
  }

  /**
   * Tests for no items checked out.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testNoItemsOut() throws Exception {
    String libUrl = "http://uhmanoa.lib.hawaii.edu/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=pbLogon";
    String formName = "logonpage";
    String param1 = "BC";
    String param2 = "LN";
    String id = "15352644";
    String lastName = "wolff";

    LibraryVault uhManoa;
    uhManoa = new LibraryVault(libUrl, formName, param1, param2);
    DueDatesGold dueDate = new DueDatesGold();

    String[] uhInfo = dueDate.getUhInfo(uhManoa, id, lastName);
    assertNull("You have no items currently checked out.", uhInfo);

  }

  /**
   * Tests for items checked out from UH Manoa Library and validates that the information is
   * correct.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testItemsOut() throws Exception {

    String uhUrl = "http://uhmanoa.lib.hawaii.edu/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=pbLogon";
    // creating libraryVault object for UH Manoa Library
    LibraryVault uhManoa;
    uhManoa = new LibraryVault(uhUrl, "logonpage", "BC", "LN");
    DueDatesGold dueDate = new DueDatesGold();

    String[] uhInfo = dueDate.getUhInfo(uhManoa, "15516494", "sanchez");
    assertEquals("Testing 2 books checked", 6, uhInfo.length);
    assertEquals("Testing Content",
        "McCarroll, Les.  Is there life after unemployment? : a survival"
            + " guide while you are unemployed / Les McCarroll.   Location: UH Manoa: Hamilton "
            + "HF5382.7 .M37 1984", uhInfo[0]);
    assertEquals("Testing Content", "Book", uhInfo[1]);
    assertEquals("Testing Content", "Checked Out: Due 11-12-08", uhInfo[2]);

  }

  /**
   * Tests login cases for the login method. Tests correct login info as well as incorrect login
   * info.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testLogin() throws Exception {

    String uhUrl = "http://uhmanoa.lib.hawaii.edu/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=pbLogon";
    // creating libraryVault object for UH Manoa Library
    LibraryVault uhManoa;
    uhManoa = new LibraryVault(uhUrl, "logonpage", "BC", "LN");
    DueDatesGold dueDate = new DueDatesGold();

    // correct login
    WebConversation conversation = dueDate.login(uhManoa, "15352644", "wolff");
    WebResponse response = conversation.getCurrentPage();
    assertNotNull("Testing correct Login", conversation);
    assertTrue("Testing correct Login Display", response.getText().contains("Checked Out Items"));

    // incorrect login
    conversation = dueDate.login(uhManoa, "1552644", "wolff");
    assertNull("Testing incorrect Login", conversation);

  }

}